package edu.xmut.yuepai.sys.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xmut.yuepai.model.entity.SysForte;
import edu.xmut.yuepai.model.vo.FortesVO;
import edu.xmut.yuepai.sys.mapper.SysForteMapper;
import edu.xmut.yuepai.sys.service.SysForteService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author shuofeng
 * @description 针对表【sys_forte(特长表)】的数据库操作Service实现
 * @createDate 2024-10-07 17:02:43
 */
@Service
public class SysForteServiceImpl extends ServiceImpl<SysForteMapper, SysForte>
        implements SysForteService {

    @Override
    public List<FortesVO> getAll() {
        // 查出所有的记录
        List<SysForte> data = this.list();
        // 递归查出子节点
        ArrayList<FortesVO> result = new ArrayList<>();
        for (SysForte currentForte : data) {
            if (currentForte.getParentId() == 0) {
                FortesVO fortesVO = new FortesVO();
                fortesVO.setId(currentForte.getId());
                fortesVO.setName(currentForte.getContent());
                // 查找子节点
                findChildren(currentForte.getId(), fortesVO, data);
                result.add(fortesVO);
            }
        }
        return result;
    }

    private void findChildren(Long parentId, FortesVO currentForte, List<SysForte> data) {
        List<FortesVO> result = data.stream()
                .filter(item -> Objects.equals(parentId, item.getParentId()))
                .map(item -> {
                    FortesVO fortesVO = new FortesVO();
                    fortesVO.setId(item.getId());
                    fortesVO.setName(item.getContent());
                    return fortesVO;
                }).toList();
        currentForte.setChildren(result);
    }
}




